package org.dtrd.modules.doctor.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.doctor.entity.bean.WalletHistoryDetail;
import org.dtrd.modules.doctor.entity.bean.WalletHistoryListInfo;
import org.dtrd.modules.doctor.entity.bean.WalletHistoryListRequest;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdWalletHistory;
import org.dtrd.modules.doctor.mapper.DtrdEntRdWalletHistoryMapper;
import org.dtrd.modules.doctor.service.IDtrdEntRdWalletHistoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 医生钱包历史数据 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-20
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdWalletHistoryServiceImpl extends ServiceImpl<DtrdEntRdWalletHistoryMapper, DtrdEntRdWalletHistory> implements IDtrdEntRdWalletHistoryService {

    @Override
    public List<WalletHistoryListInfo> getWalletHistoryList(WalletHistoryListRequest request) {
        LambdaQueryWrapper<DtrdEntRdWalletHistory> queryWrapper = baseQueryWrapper();
        List<DtrdEntRdWalletHistory> poList = list(queryWrapper);
        List<WalletHistoryListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(WalletHistoryListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public WalletHistoryDetail getWalletHistory(Integer dataId) {
        DtrdEntRdWalletHistory dtrdEntRdWalletHistory = getById(dataId);
        return Optional.ofNullable(dtrdEntRdWalletHistory)
                        .map(po -> new WalletHistoryDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateWalletHistory(WalletHistoryDetail dto) {
        DtrdEntRdWalletHistory dtrdEntRdWalletHistory = new DtrdEntRdWalletHistory();
        if(dto.getDataId() == null){
            dtrdEntRdWalletHistory = dtrdEntRdWalletHistory.init();
        }
        dtrdEntRdWalletHistory.parseFromDto(dto);
        return saveOrUpdate(dtrdEntRdWalletHistory);
    }

    @Override
    public boolean removeWalletHistory(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdWalletHistory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdWalletHistory.class)
                        .eq(DtrdEntRdWalletHistory::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdWalletHistory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdWalletHistory.class)
                        .eq(DtrdEntRdWalletHistory::getIsDel, 0);
    }
}
